package xml;
import java.io.File;
import java.io.IOException;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLParse {

	String filePath;

	public XMLParse(String filePath) {
		this.filePath = filePath;
	}

	public Map<String, String> parse() throws IOException,
			ParserConfigurationException, SAXException {
		File f = new File(filePath);

		if (f.exists()) {

			Map<String, String> map = new HashMap<String, String>();
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(f);
			
			Element root = doc.getDocumentElement();
			
			NodeList childList=root.getChildNodes();

			if(childList!=null){
				for(int i=0;i<childList.getLength();i++){
					Node node=childList.item(i);
					if(node instanceof Element){
						if(!map.containsKey(node.getNodeName()))
							map.put(node.getNodeName(), node.getTextContent());
					}
				}
			}
			
			return map;
			
		} else {
			throw new IOException("The XML file doesn't exist.");
		}
	}

}
